package com.eu.chipin;

import java.util.ArrayList;
import java.util.List;

import com.eu.chipin.backend.ContactPicker;
import com.eu.chipin.backend.DataType;
import com.eu.chipin.backend.Member;
import com.eu.chipin.backend.Trip;
import com.eu.chipin.backend.db.DB;

import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.Menu;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ListView;

public class MainActivity extends Activity {

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		
		final List<Trip> trips = DB.getTrips(MainActivity.this);
	    ArrayAdapter<Trip> tripsAdapter = new ArrayAdapter<Trip>(MainActivity.this, android.R.layout.simple_list_item_1, trips);
	    ListView tripList = (ListView)findViewById(R.id.listView1);
	    tripList.setAdapter(tripsAdapter);
	    
	    tripList.setOnItemClickListener(new OnItemClickListener()
        {

          public void onItemClick(AdapterView<?> arg0, View view, int position, long id)
          {
            Intent intent = new Intent(MainActivity.this, ViewTripActivity.class);
            intent.putExtra("trip", trips.get(position));
            startActivity(intent);
          }
        });

		Button newTripButton = (Button)findViewById(R.id.button1);

		newTripButton.setOnClickListener(new View.OnClickListener() {

			public void onClick(View view) {
				Intent intent = new Intent(MainActivity.this, NewTripActivity.class);
				startActivityForResult(intent, 1);
			}
		});
	}
	
	public void onActivityResult(int requestCode, int resultCode, Intent data) {
	    if (resultCode == RESULT_OK) {
	      Intent intent = new Intent(MainActivity.this, MainActivity.class);
          startActivity(intent);
          finish();
	    }
	  }
	
	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		getMenuInflater().inflate(R.menu.activity_main, menu);
		return true;
	}
}
